package cn.usian.jyzx.order.controller;

import cn.usian.jyzx.model.dto.OrderInfoDto;
import cn.usian.jyzx.model.entity.order.OrderInfo;
import cn.usian.jyzx.model.vo.common.Result;
import cn.usian.jyzx.model.vo.common.ResultCodeEnum;
import cn.usian.jyzx.model.vo.h5.TradeVo;
import cn.usian.jyzx.order.service.OrderService;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 11 07 14 27
 **/

@RestController
public class OrderController {

    @Autowired
    OrderService orderService;





    /**
     * /api/order/orderInfo/auth/trade
     */



    @GetMapping("/api/order/orderInfo/auth/trade")
    public Result<TradeVo> orderInfo(HttpServletRequest request){
        TradeVo tradeVo = orderService.trade(request.getHeader("token"));
        return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * /api/order/orderInfo/auth/submitOrder
     * 下单接口
     */


    @PostMapping("/api/order/orderInfo/auth/submitOrder")
    public Result submitOrder(HttpServletRequest request,
                              @RequestBody OrderInfoDto orderInfoDto){
        String token = request.getHeader("token");
        String orderId = orderService.submitOrder(token, orderInfoDto);

        return Result.build(orderId, ResultCodeEnum.SUCCESS);
    }

    /**
     *
     * 获取订单详情接口
     * /api/order/orderInfo/auth/{orderId}
     */



    @GetMapping("/api/order/orderInfo/auth/{orderId}")
    public Result orderInfo(@PathVariable Long orderId){

        OrderInfo orderInfo = orderService.orderInfo(orderId);
        return Result.build(orderInfo, ResultCodeEnum.SUCCESS);
    }

/**
 * /api/order/orderInfo/auth/buy/{skuId}
 * 立即购买
 */

@GetMapping("/api/order/orderInfo/auth/buy/{skuId}")
    public Result  buy(@PathVariable Long skuId,HttpServletRequest request){

    TradeVo tradeVo = orderService.buy(skuId,request.getHeader("token"));

    return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 订单列表接口
     * /api/order/orderInfo/auth/1/10?orderStatus=
     */



    @GetMapping("/api/order/orderInfo/auth/{pageNo}/{pageSize}")
    public Result orderList(@PathVariable("pageNo") Integer pageNo,
                            @PathVariable("pageSize") Integer pageSize,
                            Integer orderStatus,HttpServletRequest request){

        PageInfo<OrderInfo> pageInfo =  orderService.orderList(pageNo,pageSize,orderStatus,request.getHeader("token"));

        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);

    }



    @GetMapping("/api/order/getOrderInfo/{orderNo}")
    public OrderInfo getOrderInfo(@PathVariable
                                      String orderNo){

    return orderService.getOrderInfo(orderNo);
    }


    @GetMapping("/api/order/updateOrderStatus/{orderNo}/{status}")
    public int updateOrderStatus(@PathVariable("orderNo") String orderNo,
                                 @PathVariable("status")Integer status){

       return  orderService.updateOrderStatus(orderNo,status);

    }
}
